<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>SendMessage.c</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('page_tutorial_c_sendmessage.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">SendMessage.c </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment"> * This tutorial demonstrates how to open a CAN channel and send a message on it.</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> */</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"></div><div class="line"><span class="comment">// To use CANlib, we need to include canlib.h and also link to canlib32.lib</span></div><div class="line"><span class="comment">// when compiling.</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="canlib_8h.html">canlib.h</a>&quot;</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">// The check method takes a canStatus (which is an enumerable) and the method</span></div><div class="line"><span class="comment">// name as a string argument. If the status is an error code, it will print it.</span></div><div class="line"><span class="comment">// Most Canlib method return a status, and checking it with a method like this</span></div><div class="line"><span class="comment">// is a useful practice to avoid code duplication.</span></div><div class="line"><span class="keywordtype">void</span> Check(<span class="keyword">const</span> <span class="keywordtype">char</span>* <span class="keywordtype">id</span>, <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat)</div><div class="line">{</div><div class="line">  <span class="keywordflow">if</span> (stat != <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">    <span class="keywordtype">char</span> buf[50];</div><div class="line">    buf[0] = <span class="charliteral">&#39;\0&#39;</span>;</div><div class="line">    <a class="code" href="group__can__general.html#ga01a7a415c95c579750bcdd95a1d245c4">canGetErrorText</a>(stat, buf, <span class="keyword">sizeof</span>(buf));</div><div class="line">    printf(<span class="stringliteral">&quot;%s: failed, stat=%d (%s)\n&quot;</span>, <span class="keywordtype">id</span>, (<span class="keywordtype">int</span>)stat, buf);</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[]){</div><div class="line">  <span class="comment">// Holds a handle to the CAN channel</span></div><div class="line">  <a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> hnd;</div><div class="line"></div><div class="line">  <span class="comment">// Status returned by the Canlib calls</span></div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat;</div><div class="line"></div><div class="line">  <span class="comment">// The CANlib channel number we would like to use</span></div><div class="line">  <span class="keywordtype">int</span> channel_number = 0;</div><div class="line"></div><div class="line">  <span class="comment">// The msg will be the body of the message we send on the CAN bus.</span></div><div class="line">  <span class="keywordtype">char</span> msg[8] = {1, 2, 3, 4, 5, 6, 7, 8};</div><div class="line"></div><div class="line">  <span class="comment">// The first thing we need to do is to initialize the Canlib library. This</span></div><div class="line">  <span class="comment">// always needs to be done before doing anything with the library.</span></div><div class="line">  <a class="code" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary</a>();</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;Opening channel %d\n&quot;</span>, channel_number);</div><div class="line"></div><div class="line">  <span class="comment">// Next, we open up the channel and receive a handle to it. Depending on what</span></div><div class="line">  <span class="comment">// devices you have connected to your computer, you might want to change the</span></div><div class="line">  <span class="comment">// channel number. The canOPEN_ACCEPT_VIRTUAL flag means that it is ok to</span></div><div class="line">  <span class="comment">// open the selected channel, even if it is on a virtual device.</span></div><div class="line">  hnd = <a class="code" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel</a>(channel_number, <a class="code" href="canlib_8h.html#a8e4ead14e67e49e27674c706e0d88f23">canOPEN_ACCEPT_VIRTUAL</a>);</div><div class="line"></div><div class="line">  <span class="comment">// If the call to canOpenChannel is successful, it will return an integer</span></div><div class="line">  <span class="comment">// which is greater than or equal to zero. However, is something goes wrong,</span></div><div class="line">  <span class="comment">// it will return an error status which is a negative number.</span></div><div class="line">  <span class="keywordflow">if</span>(hnd &lt; 0){</div><div class="line"></div><div class="line">    <span class="comment">// To check for errors and print any possible error message, we can use the</span></div><div class="line">    <span class="comment">// Check method.</span></div><div class="line">    Check(<span class="stringliteral">&quot;canOpenChannel&quot;</span>, (<a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a>)hnd);</div><div class="line"></div><div class="line">    <span class="comment">// and then exit the program.</span></div><div class="line">    exit(1);</div><div class="line">  }</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;Setting bitrate and going bus on\n&quot;</span>);</div><div class="line"></div><div class="line">  <span class="comment">// Once we have successfully opened a channel, we need to set its bitrate. We</span></div><div class="line">  <span class="comment">// do this using canSetBusParams. CANlib provides a set of predefined bus parameter</span></div><div class="line">  <span class="comment">// settings in the form of canBITRATE_XXX constants. For other desired bus speeds</span></div><div class="line">  <span class="comment">// bus paramters have to be set manually.</span></div><div class="line">  <span class="comment">// See CANlib documentation for more information on parameter settings.</span></div><div class="line">  stat = <a class="code" href="group___c_a_n.html#ga7eb8c2e92cfae57e7ec5031818524301">canSetBusParams</a>(hnd, <a class="code" href="canlib_8h.html#a87f618db13ec4f48610def72cfb40969">canBITRATE_250K</a>, 0, 0, 0, 0, 0);</div><div class="line">  Check(<span class="stringliteral">&quot;canSetBusParams&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">// Next, take the channel on bus using the canBusOn method. This needs to be</span></div><div class="line">  <span class="comment">// done before we can send a message.</span></div><div class="line">  stat = <a class="code" href="group___c_a_n.html#ga99c7c99cc71580f8099a1407f4f9ea1a">canBusOn</a>(hnd);</div><div class="line">  Check(<span class="stringliteral">&quot;canBusOn&quot;</span>, stat);</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;Writing a message to the channel and waiting for it to be sent \n&quot;</span>);</div><div class="line"></div><div class="line">  <span class="comment">// We send the message using canWrite. This method takes five parameters:</span></div><div class="line">  <span class="comment">// the channel handle, the message identifier, the message body, the message</span></div><div class="line">  <span class="comment">// length (in bytes) and optional flags.</span></div><div class="line">  stat = <a class="code" href="group___c_a_n.html#ga62c185329d6741c90102511e2f37983e">canWrite</a>(hnd, 123, msg, 8, 0);</div><div class="line">  Check(<span class="stringliteral">&quot;canWrite&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">// After sending, we wait for at most 100 ms for the message to be sent, using</span></div><div class="line">  <span class="comment">// canWriteSync.</span></div><div class="line">  stat = <a class="code" href="group___c_a_n.html#ga304cb3a7bc2874c1f8ad361a911bcd5f">canWriteSync</a>(hnd, 100);</div><div class="line">  Check(<span class="stringliteral">&quot;canWriteSync&quot;</span>, stat);</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;Going off bus and closing channel&quot;</span>);</div><div class="line"></div><div class="line">  <span class="comment">// Once we are done using the channel, we go off bus using the</span></div><div class="line">  <span class="comment">// canBusOff method. It take the handle as the only argument.</span></div><div class="line">  stat = <a class="code" href="group___c_a_n.html#gaf1786cfbfd542b18b9c599d278837bd9">canBusOff</a>(hnd);</div><div class="line">  Check(<span class="stringliteral">&quot;canBusOff&quot;</span>, stat);</div><div class="line"></div><div class="line">  <span class="comment">// We also close the channel using the canCloseChannel method, which take the</span></div><div class="line">  <span class="comment">// handle as the only argument.</span></div><div class="line">  stat = <a class="code" href="group___c_a_n.html#ga49525373a4d08d93c651ec10f79dd36b">canClose</a>(hnd);</div><div class="line">  Check(<span class="stringliteral">&quot;canClose&quot;</span>, stat);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment"> Exercises:</span></div><div class="line"><span class="comment">  - The canWriteWait method combines canWrite with canWriteSync. Try it out.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">  - Use some other program (such as Kvaser CanKing) to listen for messages on</span></div><div class="line"><span class="comment">    a different channel on the same device as the one used in your</span></div><div class="line"><span class="comment">    program. Make sure to use the same bitrate.</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">  - Change the fourth parameter in the call to canWrite to 4. What happens to</span></div><div class="line"><span class="comment">    the message on the receiving side?</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">  - Change the message identifier to something large, like 10000. What happens</span></div><div class="line"><span class="comment">     on the receiving side? Then, change the fifth parameter to</span></div><div class="line"><span class="comment">     canMSG_EXT. What happens now?</span></div><div class="line"><span class="comment"> */</span></div></div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="page_tutorial.html">Tutorials</a></li>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
